package cc.mycraft.pvp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;


public class WeeklyReward {
	private static FileConfiguration config = Main.getPlugin().config;
	private Thread T;
	
	public static void checkWeeklyReward(Player p) {
		//不是前三
		if(!config.contains("WeeklyReward."+p.getName()+"__"+p.getUniqueId())) {
			return;
		}
		//已领过
		if(config.getBoolean("WeeklyReward."+p.getName()+"__"+p.getUniqueId())) {
			return;
		}
		//背包已满
		if(!ArenaShop.hasAvaliableSlot(p, 1)) {
			Title t = new Title("§4请腾出背包空间", "§c重进竞技场领取积分榜前三奖励", 5, 5, 40);
			t.send(p);
			return;
		}
		//发圣剑
		Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm items give "+p.getName()+" ArenaSword 1");
		config.set("WeeklyReward."+p.getName()+"__"+p.getUniqueId(), true);
		Main.getPlugin().saveConfig();
		Title t = new Title("§6§l获得圣剑！", "§e竞技场周结算奖励", 5, 5, 40);
		t.send(p);
	}
	
	protected void setWeeklyReward() {
		Set<String> Section = PlayerManager.PPoint.getConfigurationSection("Points").getKeys(false);
		config.set("WeeklyReward", null);
		Main.getPlugin().saveConfig();
		T = new Thread(new Runnable() {
			@Override
			public void run() {
				HashMap<String,Integer> hm = new HashMap<String,Integer>();
				//载入数据
				for(String playerSection:Section) {
					int Point = PlayerManager.PPoint.getInt("Points."+playerSection);
					hm.put(playerSection, Point);
				}
				ByValueComparator bvc  =new ByValueComparator(hm);
				List<String> newList=new ArrayList<String>(hm.keySet());
				Collections.sort(newList, bvc);
				//将前三存入config，然后登录时检测。
				String resultInfo = "竞技场结算完成。\n";
				for(int i =0;i<3;i++) {
					resultInfo = resultInfo+ "第"+(i+1)+"名：" + newList.get(i).substring(0, newList.get(i).length()-38)+"  ";
					if(config.contains("WeeklyReward."+newList.get(i))) {
						continue;
					}
					config.set("WeeklyReward."+newList.get(i), false);
					Main.getPlugin().saveConfig();
				}
				Bukkit.getLogger().info(resultInfo);
				PlayerManager.clearPoint();
				RankingList.clearRanking();
				Bukkit.getLogger().info("积分榜和玩家积分已清除。");
				return;
			}
		});
		T.start();
	}
	static class ByValueComparator implements Comparator<String> {

		HashMap<String, Integer> base_map;

		public ByValueComparator(HashMap<String, Integer> base_map) {
			this.base_map = base_map;
		}

		public int compare(String arg0, String arg1) {
			if (!base_map.containsKey(arg0) || !base_map.containsKey(arg1)) {
				return 0;
			}

			if (base_map.get(arg0) < base_map.get(arg1)) {
				return 1;
			}else if (base_map.get(arg0) == base_map.get(arg1)) {
				return 0;
			}else {
				return -1;
			}
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
